package com.example.a053checkbox_switch_radiobutton_imageview;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class RadioButtonActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {
    private RadioGroup mRadioGroup1;
    private RadioGroup mRadioGroup2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_radio_button);
        initView();
    }

    private void initView() {
        mRadioGroup1 = findViewById(R.id.radioGroup1);
        mRadioGroup1.setOnCheckedChangeListener(this);
        mRadioGroup1.check(R.id.radioButton3);
        mRadioGroup2 = findViewById(R.id.radioGroup2);
        mRadioGroup2.setOnCheckedChangeListener(this);
        mRadioGroup2.check(R.id.radioButton5);
    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int i) {
        String strMsg = "";
        RadioButton mRadioButton = findViewById(radioGroup.getCheckedRadioButtonId());
        if (radioGroup.getId() == R.id.radioGroup1) {
            strMsg = "选择的学历为:" + mRadioButton.getText().toString();
        }
        if (radioGroup.getId() == R.id.radioGroup2) {
            strMsg = "选择的性别为:" + mRadioButton.getText().toString();
        }
        Toast.makeText(this, strMsg, Toast.LENGTH_SHORT).show();
    }
}